﻿using CaiGou.Common.DotNetFile;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UpYunLibrary;

namespace YunTest.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            return RedirectToAction("ypyun");
        }

        public ActionResult ypyun()
        {

            string username = "adminupload";
            string password = "2018caigou";
            

            //ViewData["Policy"] = Convert.ToBase64String({ "bucket":"caigoupic-1","save-key":"/demo.jpg","expiration":"1478674618","date":"Tue, 29 May 2018 01:58:47 GMT"});

            //ViewData["Policy"] = "eyAiYnVja2V0IjoiY2FpZ291cGljLTEiLCJzYXZlLWtleSI6Ii9kZW1vLmpwZyIsImV4cGlyYXRpb24iOiIxNDc4Njc0NjE4IiwiZGF0ZSI6IlR1ZSwgMjkgTWF5IDIwMTggMDE6NTg6NDcgR01UIn0=";

            //ViewData["Signature"] = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(CaiGou.Common.Encryption.HMAC_SHA1(CaiGou.Common.Encryption.MD5(password), "POST&/caigoupic-1/&" + dt)));



            ViewData["Policy"] = "eyAiYnVja2V0IjoiY2FpZ291cGljLTEiLCJzYXZlLWtleSI6Ii90ZXN0LyIsImV4cGlyYXRpb24iOiIxNDc4Njc0NjE4IiwiZGF0YSI6IlRodSwgMzEgTWF5IDIwMTggMDc6MTA6MTYgR01UIn0=";

            string dt = DateTime.Now.ToUniversalTime().ToString("r");
            ViewData["authorization"] = "UPYUN "+username+":"+Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(CaiGou.Common.Encryption.HMAC_SHA1(CaiGou.Common.Encryption.MD5(password),"POST&/caigoupic-1&"+dt)));

            return View();
        }

     
        public ActionResult  Uploadfile()
        {
            //获取单个file
            //UpYun upyun = new UpYun("caigoupic-1", "adminupload", "2018caigou");
            //用作提供对单个文件的访问的类的基类/已由客户端上传(支持单个文件上传)
            //HttpPostedFileBase img = Request.Files["myfile"];
            //string Fname = img.FileName;
            //byte[] filetype = CG.Base.FileHelper.FileToBytes(Fname);
            //bool a = upyun.writeFile("/test/" + Fname.ToString(), filetype, true);


            //获取多个file
            UpYun upyun = new UpYun("caigoupic-1", "adminupload", "2018caigou");
            //作为提供对已上传文件的访问的类的基类(支持多个上传)。
            HttpFileCollectionBase httpRequest = Request.Files;
            for (int i = 0; i < httpRequest.Count; i++)
            {  
                //获取文件扩展名
                string extension = Path.GetExtension(Request.Files[i].FileName);
                //获取文件长度
                int fileLength = httpRequest[i].ContentLength;
                //获取文件名
                string fileName = Path.GetFileName(httpRequest[i].FileName);

                //流转化为二进制/InputStream:为上传文件准备读取文件的内容
                byte[] filetype =FileHelper.StreamToBytes(httpRequest[i].InputStream);
                //又拍云方法
                bool a = upyun.writeFile("/test/" + fileName, filetype, true);

            }
            return Content("");
    }


        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}